﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace CasinoProject
{
    /// <summary>
    /// Klasa strony do rejestrowania uzytkowników
    /// </summary>
	public partial class PageRegister
	{
        /// <summary>
        /// BackgroundWorker do rejestracji
        /// </summary>
        System.ComponentModel.BackgroundWorker BGWRegister = new System.ComponentModel.BackgroundWorker();        
        /// <summary>
        /// Stworzony użytkownik
        /// </summary>
        Admin.CasinoUser casinoUsr = new CasinoProject.Admin.CasinoUser();

        /// <summary>
        /// Status operacji
        /// </summary>
        Admin.MembershipCreateStatus status = new CasinoProject.Admin.MembershipCreateStatus();

        /// <summary>
        /// Konstruktor
        /// </summary>
		public PageRegister()
		{
			this.InitializeComponent();
            Init();
			// Insert code required on object creation below this point.
		}

        /// <summary>
        /// Inicjuje używane zmienne
        /// </summary>
        void Init()        
        {
            BGWRegister.DoWork += new System.ComponentModel.DoWorkEventHandler(BGWRegister_DoWork);
            BGWRegister.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(BGWRegister_RunWorkerCompleted);
        }


        /// <summary>
        /// Event obsłujący zakończenie rejestracji
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BGWRegister_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            if (status == Admin.MembershipCreateStatus.Success)
            {
                MessageBox.Show("Registration prosess: Success!");
                this.NavigationService.GoBack();
            }
            else
            {
                MessageBox.Show("Registration prosess: " + status.ToString());
            }            
        }

        /// <summary>
        /// Event obsługujący rejestrację
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BGWRegister_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            Register();
        }

        /// <summary>
        /// Funkcja dokonująca rejestracji na serwerze
        /// </summary>
        private void Register()
        {
            Admin.AdminClient client = new CasinoProject.Admin.AdminClient();
            try
            {
                client.ClientCredentials.UserName.UserName = "register";
                client.ClientCredentials.UserName.Password = "register";
                status = client.AddUser(casinoUsr);
            }
            catch (Exception ex)
            {

                MessageBox.Show("Błąd podczas rejestrowania: " + ex.Message);
            }
            finally
            {
                if (client != null && client.State == System.ServiceModel.CommunicationState.Opened)
                    client.Close();
            }
        }


        /// <summary>
        /// Obsługa przycisku CreateProfile
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butCreateProfile_Click(object sender, RoutedEventArgs e)
        {
            if (!BGWRegister.IsBusy)
            {
                if (BoxPassEnter.Password == BoxPassRepeat.Password)
                {
                    casinoUsr.user_name = txtName.Text;
                    casinoUsr.user_pass = BoxPassEnter.Password;//txtPass.Text;
                    casinoUsr.user_surname = txtSurname.Text;
                    casinoUsr.nick = txtNick.Text;
                    casinoUsr.login = txtLogin.Text;
                    casinoUsr.email = txtmail.Text;
                    casinoUsr.country = txtCountry.Text;
                    casinoUsr.creation_date = DateTime.Now;
                    //casinoUsr.birth_date = Convert.ToDateTime(txtDay_Copy1.Text + "-" + txtMonth.Text + "-" + txtDay.Text);
                    BGWRegister.RunWorkerAsync();
                }
                else
                    MessageBox.Show("Wrong repeated password");
            }
        }

        /// <summary>
        /// Obsługa przycisku Cancel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butCancel_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.GoBack();
        }
	}
}